package com.tvunetworks.device.extra;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.*;
import org.springframework.http.MediaType;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
import org.springframework.http.converter.xml.SourceHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * @author Etan Yang
 * @create: 2021/11/19 14:15
 */

@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication(scanBasePackages = "com.tvunetworks",exclude = {DataSourceAutoConfiguration.class})
@ComponentScan({"com.tvunetworks", "com.tvunetworks.device.extra", "com.tvunetworks.center.common"})
@PropertySources({
        @PropertySource("classpath:build.properties"),
        @PropertySource("classpath:common-build.properties")})
public class DeviceExtraApplication {
    @Value("${restTemplate.connectionTimeout}")
    private int connectionTimeout;

    @Value("${restTemplate.readTimeout}")
    private int readTimeout;

    /**
     * Startup method
     * @param args Startup param
     */
    public static void main(String[] args) {
        SpringApplication.run(DeviceExtraApplication.class);
    }


    @Bean(name="restTemplate")
    @Primary
    RestTemplate restTemplate() {
        List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
        messageConverters.add(new ByteArrayHttpMessageConverter());
        messageConverters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
        messageConverters.add(new ResourceHttpMessageConverter(Boolean.FALSE));
        messageConverters.add(new SourceHttpMessageConverter<>());
        messageConverters.add(new AllEncompassingFormHttpMessageConverter());
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL));
        messageConverters.add(mappingJackson2HttpMessageConverter);

        SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory();

        // Set timeout
        simpleClientHttpRequestFactory.setReadTimeout(readTimeout * 1000);
        simpleClientHttpRequestFactory.setConnectTimeout(connectionTimeout * 1000);


        RestTemplate restTemplate = new RestTemplate(simpleClientHttpRequestFactory);
        restTemplate.setMessageConverters(messageConverters);
        return restTemplate;
    }
}
